<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	Class.forName("com.mysql.jdbc.Driver");
	request.setCharacterEncoding("UTF-8");
	String room_id = request.getParameter("room_id");
	
	String customer_card = request.getParameter("customer_card");
	String customer_name = request.getParameter("customer_name");
	String customer_address = request.getParameter("customer_address");
	
	String indate = request.getParameter("in_date");
	String number = request.getParameter("number");
	String price_1 = request.getParameter("price_1");
	String price_2 = request.getParameter("price_2");
	String price_3 = request.getParameter("price_3");
	
	if (room_id.length()==0) {
		response.sendRedirect("ru_detail.jsp?id="+room_id);
	}
	
// 	System.out.println(service_id+" "+service_name+" "+service_price+" "+service_number);
	Connection con = null;
	Statement smt = null;
	ResultSet rst = null;
	ResultSet rst1 = null;
	String SQL = null;
	try {
		String username = "root";
		String password = "Vietyeutam1112";
		String url = "jdbc:mysql://127.0.0.1:3306/ghmotel?useUnicode=true&characterEncoding=UTF-8";
		con = DriverManager.getConnection(url, username, password);
		System.out.println("Da ket noi CSDL");
		smt = con.createStatement();
		SQL = "UPDATE room_using SET customer_card="+customer_card+", in_date='"+indate+
				"', number="+number+", price_1="+price_1+", price_2="+price_2+", price_3="+price_3+" WHERE room_id="+room_id+"";

		String strSqlAdd = String.format("CALL insertRoom_using(%s,%s,'%s',%s,%s,%s,%s)", room_id, customer_card, indate, number, price_1, price_2, price_3);
		
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery("CALL getRoomInfo("+ room_id + ")");
		
		int isAdd = 1;
		
		while (rs.next()) {
			isAdd = 2;
		}

		if (isAdd == 2) {
			smt.execute(SQL);
		} else {
			smt.executeQuery(strSqlAdd);
		}
		
		con.close();
		System.out.println("Dong ket noi");
		response.sendRedirect("index.jsp");
	}
	catch (Exception e) {
		e.printStackTrace();
		System.out.println("KHONG KET NOI DUOC");
	}

%>
</body>
</html>